{% extends 'admin/common/base.html' %}
{% block css %}
{{super()}}
<link href="{{url_for('admin.static',filename='ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css')}}" rel="stylesheet"/>
{% endblock %}
{% block body %}
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-role-add">
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">角色名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleName" id="roleName" required>
				</div>
			</div>
			<div class="form-group" style="display: none;">
				<label class="col-sm-3 control-label is-required">权限字符：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleKey" id="roleKey" required>
					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限字符，如：@RequiresRoles("")</span>
				</div>
			</div>
            <div class="form-group">
				<label class="col-sm-3 control-label">权限等级：</label>
				<div class="col-sm-8">
					<select id="scope" name="scope" class="form-control m-b">
                        {% if current_user.role %}
                        {% for s in current_user.role.scopes() %}
						<option value="{{s.id}}">{{s.name}}</option>
                        {% endfor %}
                        {% else %}
                        <option value="1">超管权限</option>
                        <option value="2">本部门及子部门权限</option>
                        <option value="3">本部门权限</option>
                        <option value="4">个人权限</option>
                        {% endif %}
					</select>
					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 可访问的数据权限</span>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleSort" id="roleSort" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
			        <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" checked>
                        <span></span>
                    </label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单权限：</label>
				<div class="col-sm-8">
				    <label class="check-box">
				        <input type="checkbox" value="1">展开/折叠</label>
				    <label class="check-box">
				        <input type="checkbox" value="2">全选/全不选</label>
				    <label class="check-box">
				        <input type="checkbox" value="3" checked>父子联动</label>
				    <div id="menuTrees" class="ztree ztree-border"></div>
				</div>
			</div>
		</form>
	</div>
</body>
{% endblock %}
{% block js %}
{{ super() }}
<script src="{{url_for('admin.static',filename='ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js')}}"></script>
<script type="text/javascript">
    var ctx = '';
    $(function() {
        var url = "{{url_for('admin.sys.menu.role_tree_data')}}";
        var options = {
            id: "menuTrees",
            url: url,
            check: { enable: true },
            expandLevel: 0
        };
        $.tree.init(options);
    });
    
    $("#form-role-add").validate({
        rules:{
            onkeyup: false,
            roleName:{
                // remote: {
                //     url: ctx + "system/role/checkRoleNameUnique",
                //     type: "post",
                //     dataType: "json",
                //     data: {
                //         "roleName" : function() {
                //             return $.common.trim($("#roleName").val());
                //         }
                //     },
                //     dataFilter: function(data, type) {
                //         return $.validate.unique(data);
                //     }
                // }
            },
            roleKey:{
                // remote: {
                //     url: ctx + "system/role/checkRoleKeyUnique",
                //     type: "post",
                //     dataType: "json",
                //     data: {
                //         "roleKey" : function() {
                //             return $.common.trim($("#roleKey").val());
                //         }
                //     },
                //     dataFilter: function(data, type) {
                //         return $.validate.unique(data);
                //     }
                // }
            },
            roleSort:{
                digits:true
            },
        },
        messages: {
            "roleName": {
                remote: "角色名称已经存在"
            },
            "roleKey": {
                remote: "角色权限已经存在"
            }
        },
        focusCleanup: true
    });
    
    $('input').on('ifChanged', function(obj){
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == 1) {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "2") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
            } else {
                $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
            }
        }
    })
    
    function submitHandler() {
        if ($.validate.form()) {
            add();
        }
    }

    function add() {
        var roleName = $("input[name='roleName']").val();
        var roleKey = $("input[name='roleKey']").val();
        var scope = $('#scope').val();
        var roleSort = $("input[name='roleSort']").val();
        var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
        var remark = $("input[name='remark']").val();
        var menuIds = $.tree.getCheckedNodes();
        $.ajax({
            cache : true,
            type : "POST",
            url : "{{url_for('admin.sys.role.add')}}",
            data : {
                "name": roleName,
                "key": roleKey,
                "scope": scope,
                "sort": roleSort,
                "status": status,
                "remark": remark,
                "menuIds": menuIds
            },
            async : false,
            error : function(request) {
                $.modal.alertError("系统错误");
            },
            success : function(data) {
                $.operate.successCallback(data);
            }
        });
    }
</script>
{% endblock %}
